package com.example.baidu_map_plugin.mapview;

import android.content.Context;
import android.util.Log;

import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry;

public class FlutterNativeMapViewFactory extends FlutterNativeViewBaseFactory<FlutterNativeBaiDuMapView> {
    private static final String TAG = FlutterNativeMapViewFactory.class.getSimpleName();
    private static final String viewTypeId = "plugins.baidumapplugin_example/mapview";

    public FlutterNativeMapViewFactory(Context context, BinaryMessenger messenger) {
        super(context, messenger);
    }

    @Override
    public FlutterNativeBaiDuMapView createPlatformView(Context context, int id, Object o) {
        Log.d(TAG, "createPlatformView():initData=" + o);
        return new FlutterNativeBaiDuMapView(context, messenger, id, o);
    }

    public static void registerWith(ShimPluginRegistry shimPluginRegistry, FlutterPlugin.FlutterPluginBinding binding) {
        final String key = FlutterNativeMapViewFactory.class.getCanonicalName();
        io.flutter.embedding.engine.plugins.PluginRegistry pluginRegistry = binding.getFlutterEngine().getPlugins();

        PluginRegistry.Registrar registrarFor = null;
        if (shimPluginRegistry.hasPlugin(key)) {
            registrarFor = shimPluginRegistry.valuePublishedByPlugin(key);
        } else {
            registrarFor = shimPluginRegistry.registrarFor(key);
        }

        binding
                .getFlutterEngine()
                .getPlatformViewsController()
                .getRegistry()
                .registerViewFactory(
                        viewTypeId, new FlutterNativeMapViewFactory(registrarFor.activity(), binding.getBinaryMessenger()));

    }

    public static void registerWith(ShimPluginRegistry.Registrar registrar) {

        registrar.platformViewRegistry()
                .registerViewFactory(
                        viewTypeId, new FlutterNativeMapViewFactory(registrar.activity(), registrar.messenger()));

    }

}